package com.macro.mall.tiny.service.impl;

import com.macro.mall.tiny.component.JwtAuthenticationTokenFilter;
import com.macro.mall.tiny.dao.PortalOrderDao;
import com.macro.mall.tiny.dto.OmsOrderDetail;
import com.macro.mall.tiny.mbg.mapper.OmsOrderMapper;
import com.macro.mall.tiny.mbg.mapper.OmsOrderSettingMapper;
import com.macro.mall.tiny.mbg.mapper.SmsCouponHistoryMapper;
import com.macro.mall.tiny.mbg.model.*;

import com.macro.mall.tiny.service.OmsPortalOrderService;
import com.macro.mall.tiny.service.UmsMenberServcie;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：czx
 * @date ：Created in 2021/7/20 18:17
 * @descriptioin:
 * @modified By：
 */
@Service
@Slf4j
class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(OmsPortalOrderServiceImpl.class);

    @Autowired
    private OmsOrderMapper omsOrderMapper;
    @Autowired
    private OmsOrderSettingMapper omsOrderSettingMapper;
    @Autowired
    private PortalOrderDao portalOrderDao;
    @Autowired
    private UmsMenberServcie umsMemberService;
    @Autowired
    private SmsCouponHistoryMapper smsCouponHistoryMapper;

    /**
     * 取消超时订单
     */
    @Override
    public Integer cancelTimeOutOrder() {
        Integer count=0;
        OmsOrderSetting omsOrderSetting = omsOrderSettingMapper.selectByPrimaryKey(2L);
        //查询超时,未支付的订单及订单详情
        List<OmsOrderDetail> timeOutOrders = portalOrderDao.getTimeOutOrders(omsOrderSetting.getNormalOrderOvertime());
        LOGGER.info("提交但是未付款的订单有===={}",timeOutOrders.size());
        if (CollectionUtils.isEmpty(timeOutOrders)){
            return count;
        }
        //如果查询到了订单,修改订单状态为交易取消
        List<Long> ids = new ArrayList<>();
        for (OmsOrderDetail timeOutOrder: timeOutOrders){
            ids.add(timeOutOrder.getId());
        }
       portalOrderDao.updateOrderStatus(ids, 4);
        for (OmsOrderDetail timeOutOrder:timeOutOrders){
            //解除(release)订单商品库存锁定
            try {
                portalOrderDao.releaseSkuStockLock(timeOutOrder.getOrderItemList());
            }catch(Exception e) {
                System.out.println("这里异常========================"+e);
            }


            //修改优惠券使用状态,传参:1,优惠券id,成员id,
            updateCouponStatus(timeOutOrder.getCouponId(),timeOutOrder.getMemberId(),0);
            //返还使用积分
            if (timeOutOrder.getUseIntegration()!=null){
                UmsMember member=umsMemberService.getById(timeOutOrder.getMemberId());
                umsMemberService.updateIntegration(timeOutOrder.getMemberId(),member.getIntegration()+timeOutOrder.getUseIntegration());

            }
        }
        return timeOutOrders.size() ;
    }

    /**
     * 更改优惠券状态
     * @param couponId   优惠券id,
     * @param memberId   成员Id,
     * @param useStatus   使用状态
     */
    private void updateCouponStatus(Long couponId,Long memberId,Integer useStatus){
        if (couponId==null) {
            return;
        }
        //查询第一张优惠券
        SmsCouponHistoryExample example = new SmsCouponHistoryExample();
        example.createCriteria().andMemberIdEqualTo(memberId)
                .andCouponIdEqualTo(couponId)
                .andUseStatusEqualTo(useStatus==0?1:0);//如果传过来的值不是等于0状态的,都设置状态为0:
        List<SmsCouponHistory> couponHistoryList = smsCouponHistoryMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(couponHistoryList)){
            SmsCouponHistory smsCouponHistory = couponHistoryList.get(0);
            //设置现在 为使用优惠券时间
            smsCouponHistory.setUseTime(new Date());
            //设置为传过来的状态
            smsCouponHistory.setUseStatus(useStatus);
            //更新优惠券信息
            smsCouponHistoryMapper.updateByPrimaryKeySelective(smsCouponHistory);
        }

    }

}

















